package com.gopher.gopherbeasts;

import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Player extends Entity {

	public State state;
	public State lastState;
	public float jumpSpeed = 16f;
	public static final float FRICTION = .65f;
	Controls controls;
	World world;

	enum State {
		MOVING, RESTING, JUMPING, FALLING
	}

	public Player(float x, float y, float w, float h, World world) {
		super(x, y, w, h, new TextureRegion(Art.player));
		state = State.RESTING;
		controls = InputHandler.getControls();
		this.world = world;
	}

	@Override
	public void update() {
		lastState = state;
		accx = World.GRAVITY.x;
		accy = World.GRAVITY.y;

		if (y < 0) {
			y = 0;
			vely = 0;
		} else if (y > world.worldHeight - h) {
			y = world.worldHeight - h;
			vely = 0;
		}
		if (x < 0) {
			x = 0;
			velx = 0;
		} else if (x > world.worldWidth - w) {
			x = world.worldWidth - w;
			velx = 0;
		}

		if (controls.right.isDown) {
			accx += accelRate;
		}
		if (controls.left.isDown) {
			accx -= accelRate;
		}
		if (controls.jump.isDown) {
			if (!controls.jump.wasDown) {
				if ((state == State.RESTING || state == State.MOVING)) {
					Sound.jump.play();
					vely = jumpSpeed;
				}
			}
		} else {
			if (vely > 0) {
				vely = 0;
			}
		}

		if (vely < 0 || (vely == 0 && lastState == State.JUMPING)) {
			state = State.FALLING;
		} else if (vely > 0) {
			state = State.JUMPING;
		} else if (accx != 0) {
			state = State.MOVING;
		} else {
			state = State.RESTING;
		}

		velx = velx * FRICTION;
		// System.out.println(state);
	}

	@Override
	public void render() {

	}

	@Override
	public void collidedWith(Entity other) {
		
	}

}
